package com.jinbo.footmessage.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.SendCodeBean;
import com.jinbo.footmessage.httpbean.UserInformationBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.utils.DialogUtils;
import com.jinbo.footmessage.utils.TimeCount;
import com.jinbo.footmessage.utils.VerifyUtils;
import com.jinbo.footmessage.view.CrossEditText;
import com.jinbo.footmessage.view.TitleBarView;


public class FindPasswordActivity extends BaseActivity implements View.OnClickListener {
    private CrossEditText mPhoneEt,mPasswordEt,mCodeEt,mConPasswordEt;
    private Button mGetCodeBt,mSubmit;
    private  TimeCount time;
    private String  mCodeStr="-1";
    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, FindPasswordActivity.class);
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        initTitleBarView();
        initView();
        initListener();

    }

    public void initTitleBarView() {
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("找回密码");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
    }

    // 初始化View
    private void initView() {
        mPhoneEt = (CrossEditText) findViewById(R.id.phone_num);
        mGetCodeBt = (Button) findViewById(R.id.get_code);
        mSubmit = (Button) findViewById(R.id.submit);
        mPasswordEt = (CrossEditText) findViewById(R.id.re_pwd);
        mConPasswordEt= (CrossEditText) findViewById(R.id.con_pwd);
        mCodeEt = (CrossEditText) findViewById(R.id.code_num);
        time = new TimeCount(120000, 1000,mGetCodeBt);

    }


    // 监听
    private void initListener() {
        //submit.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
        mGetCodeBt.setOnClickListener(this);
        mPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mPhoneEt.getText().toString().trim().length() == 11) {
                        mGetCodeBt.setEnabled(true);
                } else {
                    mGetCodeBt.setEnabled(false);
                }

                submitEnable();

            }
        });
        mCodeEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mPasswordEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mConPasswordEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
    }
    void submitEnable()
    {
        if (mPhoneEt.getText().toString().trim().length() == 11
                && mPasswordEt.getText().toString().trim().length() > 0&&mConPasswordEt.getText().toString().trim().length() > 0
                && mCodeEt.getText().toString().trim().length() > 0) {
            mSubmit.setEnabled(true);
        } else {
            mSubmit.setEnabled(false);
        }
    }

    private void submitCheck() {
        String password = mPasswordEt.getText().toString().trim();
        String confirm=mConPasswordEt.getText().toString().trim();
        if (TextUtils.isEmpty(password) || !VerifyUtils.isPassword(password) || password.length() > 16 || password.length() < 6) {
            show("密码格式为6-16位数字或字母组合");
            return;
        }
        else if(!password.equals(confirm))
        {
            show("两次输入密码不一致");
            return;
        }
        else if(mGetCodeBt.getText().toString().equals("重新验证"))
        {
            show("验证码失效，重新验证");
            return;
        }
        else if(!mCodeStr.equals(mCodeEt.getText().toString()))
        {
            show("验证码错误，重新验证");
            time.reset();
            return;
        }
        else {


            BusinessController.getInstance().findPassword(mPhoneStr, password,  new Listener<UserInformationBean>() {
                @Override
                public void onStart(Object... params) {
                    showProgressDialog("请稍后...");
                }

                @Override
                public void onComplete(UserInformationBean result, Object... params) {
                    dismissProgressDialog();
                    new DialogUtils(FindPasswordActivity.this, "center",false,0)
                            .setMessage("修改密码成功！请就住新密码")
                            .setButtons(null, "返回登录",
                                    new DialogUtils.OnDialogButtonClickListener() {
                                        @Override
                                        public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                            if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {
                                                // ActivitiesHelper.getInstance().closeAll();
                                                LoginActivity.launch(FindPasswordActivity.this);
                                            }
                                        }
                                    }).create().show();
                }

                @Override
                public void onFail(String msg, Object... params) {
                    dismissProgressDialog();
                    if (TextUtils.isEmpty(msg)) {
                    } else {
                        new DialogUtils(FindPasswordActivity.this, "center", true, 0).setMessage(msg).setButtons(null, "确定", null).create().show();

                    }
                }
            });
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
           case R.id.submit:
                submitCheck();
                break;
            case R.id.get_code:
                mGetCodeBt.setEnabled(false);
                getCode();
                break;
            default:
                break;
        }
    }

    String mPhoneStr;//
    public void getCode() {
        mPhoneStr = mPhoneEt.getText().toString().trim();
        if (!VerifyUtils.isPhoneNum(mPhoneStr)) {
            //弹出提示 您输入的手机号码格式错误！
            show("您输入的手机号码格式错误！");
            return;
        } else {

            BusinessController.getInstance().sendCode(mPhoneStr, new Listener<SendCodeBean>() {

                @Override
                public void onStart(Object... params) {
                    showProgressDialog("请稍后...");
                }

                @Override
                public void onComplete(SendCodeBean result, Object... params) {
                    dismissProgressDialog();
                    //  show("验证码已发送，请注意查收");
                    time.start();
                    //countDown(COUNT);
                    mGetCodeBt.setEnabled(false);
                    mCodeStr = result.code;
                    //mCountLL.setVisibility(View.VISIBLE);
                    show("验证码已发送，请注意查收" + mCodeStr);
                }

                @Override
                public void onFail(String msg, Object... params) {
                    dismissProgressDialog();
                   if (TextUtils.isEmpty(msg)) {

                   } else {
                       new DialogUtils(FindPasswordActivity.this, "center", true, 0).setMessage(msg).setButtons(null, "确定", null).create().show();

                   }
               }
           });

        }
    }
}
